defmodule Sublist do
  @doc """
  Returns whether the first list is a sublist or a superlist of the second list
  and if not whether it is equal or unequal to the second list.
  """
  def compare(l, l) when is_list(l), do: :equal
  def compare([], l) when is_list(l), do: :sublist
  def compare(l, []) when is_list(l), do: :superlist

  def compare(a, b) do
    cond do
      a === b                                  -> :equal
      length(a) < length(b) and as_sub?(a, b) -> :sublist
      length(a) > length(b) and as_sub?(b, a) -> :superlist
      true                                     -> :unequal
    end
  end

  defp as_sub?([], _b), do: true # empty as sub. of anything list
  defp as_sub?(a, b) do
    b
    |> Stream.chunk(length(a), 1)
    |> Enum.member?(a)
  end
end
